import create from 'zustand';

interface ChatMessage {
  sender: string;
  message: string;
}

interface Chat {
  avator: string; 
  id: number;
  title: string;
  messages: ChatMessage[];
  date: string;
}

interface ChatState {
  chats: Chat[];
  selectedChatId: number;
  selectChat: (id: number) => void;
  addMessage: (chatId: number, sender: string, message: string) => void;
}

export const useChatStore = create<ChatState>((set) => ({
  chats: [
    { avator:'/1.jpg',
      id: 1,
      title: '系统消息',
      messages: [{ sender: '系统消息', message: '欢迎注册!!!', timestamp: '2024/07/31 11:02:09' }],
      date: '2024/07/31 11:02:09'
    },
    { avator:'/2.jpg',
      id: 2,
      title: '码上负责人',
      messages: [{ sender: '码上负责人', message: '欢迎你加入我们的项目...', timestamp: '2024/07/31 11:02:09' }],
      date: '2024/07/31 11:02:09'
    },
  ],
  selectedChatId: 1,
  selectChat: (id) => set({ selectedChatId: id }),
  addMessage: (chatId, sender, message) =>
    set((state) => ({
      chats: state.chats.map((chat) =>
        chat.id === chatId
          ? {
              ...chat,
              messages: [...chat.messages, { sender, message}],
            }
          : chat
      ),
    })),
}));
